<template>
  <div class="department-container">
    <div class="app-container">
      <el-card shadow="always" :body-style="{ padding: '20px' }">
        <div slot="header">
          <el-row>
            <el-col :span="20">
              <span>{{ companyInfo.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>负责人</el-col>
                <!-- 下拉菜单 element -->
                <el-col>
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDept()">
                        添加子部门
                      </el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </div>
        <!-- card body=>默认插槽指定卡片内容 -->
        <!-- :default-expand-all="true" 默认展开全部 -->
        <div>
          <el-tree
            :data="treeData"
            :props="defaultProps"
            :default-expand-all="true"
            @node-click="handleNodeClick"
          >
            <!--
            1.自定义树的结构和样式
            2.借助作用域插槽获取树节点数据
           -->
            <template #default="{ data }">
              <el-row style="width: 100%">
                <el-col :span="20">
                  <span>{{ data.name }}</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <!-- 两个内容 -->
                    <!-- 部门负责人 -->
                    <el-col>{{ data.manager || "ceo" }}</el-col>
                    <el-col>
                      <!-- 下拉菜单 element -->
                      <el-dropdown>
                        <span> 操作<i class="el-icon-arrow-down" /> </span>
                        <!-- 下拉菜单 操作按钮-->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="addDept(data)">
                            添加子部门
                          </el-dropdown-item>
                          <el-dropdown-item @click.native="editDept(data)">
                            编辑部门
                          </el-dropdown-item>
                          <el-dropdown-item @click.native="delDepet(data)">
                            删除部门
                          </el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-tree>
        </div>
      </el-card>
    </div>
    <add-dept
      ref="edit"
      :show-dialog="showDialog"
      :current-node="currentNode"
      :all-tree-date="allTreeDate"
      @close-dialog="showDialog = false"
      @update-depart="getAll"
    ></add-dept>
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/departments.js'
import { formatTreeData } from '@/utils/index.js'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components: {
    AddDept
  },
  data () {
    return {
      // 公司信息
      companyInfo: { name: '' },
      // 公司组织架构数据
      /**
       * 1.父部们的pid为空
       * 2.子部门的pid不为空且为父的id
       */
      treeData: [],
      defaultProps: {
        children: 'children',
        name: 'name'
      },
      allTreeDate: [],
      showDialog: false,
      // 当前点击部门的数据
      currentNode: null
    }
  },
  created () {
    this.getAll()
  },
  methods: {
    handleNodeClick (data) {
      // console.log(data)
    },
    async getAll () {
      const res = await getDepartments()
      // console.log('列表的数据:', res)
      this.companyInfo.name = res.companyName
      this.treeData = formatTreeData(res.depts)
      this.allTreeDate = res.depts
      // = res.depts
    },
    // 新增
    addDept (data) {
      this.currentNode = data
      this.showDialog = true
    },
    // 编辑
    editDept (data) {
      this.currentNode = data
      this.showDialog = true
      // 回填当前编辑数据需要调用接口来保证获取的是最新的数据
      // 打开的时候获取到当前编辑部门的ID=>调用接口
      // 父组件调用子组件方法=>使用ref
      this.$refs.edit.getDept(data.id)
    },
    // 删除部门
    delDepet (data) {
      this.$confirm(`你确认要删除${data.name}吗?`, '提示').then(async () => {
        if (data.children) {
          this.$message({
            message: '不可以删除父组件,请先删除子组件',
            type: 'warning'
          })
        }
        await delDepartments(data.id)
        this.$message.success('删除操作成功')
      }).catch(error => {
        console.log(error)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-tree-node__content {
  margin: 3px 0;
  font-size: 14px;
}
</style>
